home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 2 / MacMania 2.toast / Demo's / Tools&Utilities / DesktopSwitch / FileRoutines.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-18  |  3.7 KB  |  133 lines  |  [TEXT/KAHL]

  1. /* file manipulation routines not directly dependen on DS globals.
  2.  
  3.     Sam Madden
  4.     June 17, 1993
  5. */
  6.  
  7. #include "FileRoutines.h"
  8.  
  9. /*-----------------------------------FSSpecToInfoRec()-----------------------------------*/
  10. //Routine to convert an FSSpec to a CInfoPBRec.  So we can use PBGetCatInfo.
  11. OSErr FSSpecToInfoRec (FSSpec theSpec,CInfoPBRec *theInfoRec)
  12. {
  13.     OSErr theErr;
  14.     
  15.     theInfoRec->hFileInfo.ioVRefNum =         theSpec.vRefNum;
  16.     theInfoRec->hFileInfo.ioDirID =         theSpec.parID;
  17.     theInfoRec->hFileInfo.ioNamePtr =            (StringPtr) &theSpec.name;
  18.     theInfoRec->hFileInfo.ioFDirIndex =     0; //Search by name
  19.     theInfoRec->hFileInfo.ioCompletion =     NULL;
  20.     
  21.     return (PBGetCatInfo (theInfoRec,false));
  22.  
  23. }
  24.  
  25. /*-----------------------------------MakeInvisible()-----------------------------------*/
  26.  
  27. void MakeInvisible (FSSpec theSpec)
  28. {
  29.     FInfo inf;
  30.     OSErr err;
  31.     CInfoPBRec pb;
  32.     
  33.     FSSpecToInfoRec (theSpec,&pb);
  34.     err = pb.hFileInfo.ioResult;
  35.     if (err)
  36.         DebugStr ("\pCouldn't find file.");
  37.     inf = pb.hFileInfo.ioFlFndrInfo;
  38.     if (!(inf.fdFlags & fInvisible) && !err) {
  39.         inf.fdFlags |= fInvisible; //set bit 14
  40.         pb.hFileInfo.ioFlFndrInfo = inf;
  41.         pb.hFileInfo.ioNamePtr = (StringPtr) &theSpec.name;
  42.         pb.hFileInfo.ioDirID = theSpec.parID;
  43.         pb.hFileInfo.ioVRefNum =         theSpec.vRefNum;
  44.         pb.hFileInfo.ioFDirIndex =     0; //Search by name
  45.         pb.hFileInfo.ioCompletion =     NULL;
  46.         err = PBSetCatInfo (&pb,false);
  47.         if (err)
  48.             DebugStr ("\pCouldn't make file invisible.");
  49.         FlushVol (NULL,theSpec.vRefNum);
  50.     }
  51. }
  52.  
  53. /*-----------------------------------MakeVisible()-----------------------------------*/
  54.  
  55. void MakeVisible (FSSpec theSpec)
  56. {
  57.     FInfo inf;
  58.     OSErr err;
  59.     CInfoPBRec pb;
  60.     
  61.     FSSpecToInfoRec (theSpec,&pb);
  62.     err = pb.hFileInfo.ioResult;
  63.     if (err)
  64.         DebugStr ("\pCouldn't find file.");
  65.     inf = pb.hFileInfo.ioFlFndrInfo;
  66.     if ((inf.fdFlags & fInvisible) && !err) {
  67.         inf.fdFlags -= fInvisible; //unset bit 14
  68.         pb.hFileInfo.ioFlFndrInfo = inf;
  69.         pb.hFileInfo.ioNamePtr = (StringPtr) &theSpec.name;
  70.         pb.hFileInfo.ioDirID = theSpec.parID;
  71.         pb.hFileInfo.ioVRefNum =         theSpec.vRefNum;
  72.         pb.hFileInfo.ioFDirIndex =     0; //Search by name
  73.         pb.hFileInfo.ioCompletion =     NULL;
  74.         err = PBSetCatInfo (&pb,false);
  75.         if (err)
  76.             DebugStr ("\pCouldn't make file invisible.");
  77.         FlushVol (NULL,theSpec.vRefNum);
  78.     }
  79. }
  80.  
  81. OSErr MoveFileToDesktop (FSSpec theFile)
  82. {
  83.     short vref;
  84.     long ddirid,vdirid;
  85.     FSSpec dFoldSpec;
  86.     Str31 volName;
  87.     OSErr err;
  88.     
  89.     HGetVol (volName,&vref,&vdirid);
  90.     err = FindFolder (vref,kDesktopFolderType,true,&vref,&ddirid);
  91.     if (theFile.parID != ddirid){
  92.         err = FSMakeFSSpec (vref,ddirid,0,&dFoldSpec); //we expect to find this
  93.         if (err) goto done;
  94.         err = FSpCatMove (&theFile,&dFoldSpec);
  95.     }
  96.     MakeVisible (theFile); //if it is on the desktop, it may be inviisble
  97.     done:
  98.         return err;
  99. }
  100.  
  101. OSErr GetFinderInfo (FSSpec theFile,FInfo* finderInfo,FXInfo* finderXInfo)
  102. {
  103.     CInfoPBRec theInfoRec;
  104.     OSErr err;
  105.     
  106.     err = FSSpecToInfoRec (theFile,&theInfoRec);
  107.     if (err) DebugStr ("\pFSSpecToInfoRec failed.");
  108.     if (finderInfo != NULL)
  109.         *finderInfo = theInfoRec.hFileInfo.ioFlFndrInfo;
  110.     if (finderXInfo != NULL)
  111.         *finderXInfo = theInfoRec.hFileInfo.ioFlXFndrInfo;
  112.     return err;
  113. }
  114.  
  115. OSErr SetFinderInfo (FSSpec theFile,FInfo* finderInfo,FXInfo* finderXInfo)
  116.     CInfoPBRec theInfoRec;
  117.     OSErr err;
  118.     
  119.     err = FSSpecToInfoRec (theFile,&theInfoRec);
  120.     if (err) DebugStr ("\pFSSpecToInfoRec failed.");
  121.     if (finderInfo != NULL)
  122.         theInfoRec.hFileInfo.ioFlFndrInfo = *finderInfo;
  123.     if (finderXInfo != NULL)
  124.         theInfoRec.hFileInfo.ioFlXFndrInfo = *finderXInfo;
  125.     theInfoRec.hFileInfo.ioDirID = theFile.parID;
  126.     theInfoRec.hFileInfo.ioNamePtr = (StringPtr)&theFile.name;
  127.     theInfoRec.hFileInfo.ioVRefNum = theFile.vRefNum;
  128.     err = PBSetCatInfo (&theInfoRec,false);
  129.     if (err) DebugStr ("\pSetCatInfo failed.");
  130.     return err;
  131. }
  132.